/**
 * @author comahead
 */
bugs.utils.fileUploaderManager = {
	id : 1,
	fileUploader : {},
	
	add :function(obj) {
		obj.id = this.id++;
		this.fileUploader[obj.id] = obj;
	},
	
	getUniqueId : function() {
		return bugs.utils.fileUploaderManager.id++;
	},

	handleEvent : function() {
		if(arguments.length < 1) return;
		
		var args = Array.prototype.slice.call(arguments);
		var id = args.shift(); 
		
		var obj = this.fileUploader[id];
		if(obj) {
			obj.handleEvent.apply(obj, args);
		}
	}
};

bugs.utils.fileUploader = bugs.create();
bugs.utils.fileUploader.prototype = {
	containerId : "BugsFileUploaderContainer",
	
	initialize : function(selector, option) {	
		bugs.utils.fileUploaderManager.add(this);

		this.obj = $(selector).mouseover(this.reposition.bind(this));
		this.option = bugs.overed({
			fieldName : "uploadFile",
			uploadUrl : g_storageServerWrite + "/storage/upload",
			fileFilter : "all",
			maxFileSize : 5 * 1024 * 1024,
			select : function(name, size) {},
			uploadComplete : function(data) {},
			exceedFileSize : function(name, size) {},
			progress : function(loaded, total) {},
			zIndex : 100
		}, option || {});
		
		// swfobject 없으면 추가
		if(typeof(swfobject) == "undefined") {
			$.getScript(g_urlJs + "/swfobject/swfobject.js", function() {
				this.loadSwf();
			}.bind(this));
			
			return;
		}
		
		this.loadSwf();
	},
	
	loadSwf : function() {
		if($("#" + this.containerId).length > 0) {
			this.container = $("#" + this.containerId);
			this.swf = $("#BugsFileUploader")[0];
			if(this.swf.tagName == "DIV") this.swf = null; 
		}
		else {
			// firefox에서는 일단 화면에 플래쉬가 보여야 ExternalInterface.addCallback()이 되기 때문에
			// 일단 화면에 보이게끔 scrollTop으로 container를 생성함
			var top = $(document).scrollTop();
			this.container = $("<div id=\"" + this.containerId + "\"><div id=\"BugsFileUploader\"></div></div>").appendTo("body").css({position : "absolute", width : 10, height : 10, top : top, "z-index" : this.option.zIndex});
			
			var flashvars = {
				id : this.id,
				fieldName : this.option.fieldName,
				uploadUrl : this.option.uploadUrl,
				fileFilter : this.option.fileFilter,
				maxFileSize : this.option.maxFileSize,
				eventHandler : "bugs.utils.fileUploaderManager.handleEvent"
			};
			
			var params = {
				wmode : "transparent",
				allowScriptAccess : "always" 
			};
			
			var attr = {
				id : "BugsFileUploader"
			};
			
			swfobject.embedSWF(g_urlSwf + "/BugsFileUploader.swf", "BugsFileUploader", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params, attr, function(e) {
				if(e.success) this.swf = e.ref;
			}.bind(this));
		}		
	},
	
	reposition : function() {
		if(!this.swf) {
			var swf = $("#BugsFileUploader")[0];
			if(!swf || swf.tagName == "DIV") return false;
			
			this.swf = swf;
		}
		
		var offset = this.obj.offset();
		var width = this.obj.width();
		var height = this.obj.height();

		this.container.offset(offset).width(width).height(height).show();
		
		if(this.swf.setParam)
			this.swf.setParam(this.id, this.option.fieldName, this.option.uploadUrl, this.option.fileFilter, this.option.maxFileSize);
	},
	
	handleEvent : function() {
		var event = arguments[0];
		switch(event) {
			case "mouseOver " : break;
			case "mouseOut" : break;
			case "mouseDown" : break;
			case "mouseUp" : break;
			case "exceedFileSize" :
				this.option.exceedFileSize(arguments[1], arguments[2]);
				break;
			case "select" :
				var name = arguments[1];
				var size = arguments[2];
				this.option.select(name, size);
				break;
			case "progress" :
				this.option.progress(arguments[1], arguments[2]);
				break;
			case "uploadComplete" : 
				var data = arguments[1];
				this.option.uploadComplete($.parseJSON(data));
				break;
			case "error" : 
				alert("error : " + arguments[1]);
				break;
		}
	},
	
	hide : function() {
		this.container.hide();
	}
};